package com.yswl.yeeduo.ui.activity;

import java.util.Map;
import java.util.TreeMap;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.umeng.analytics.MobclickAgent;
import com.yswl.yeeduo.App;
import com.yswl.yeeduo.R;
import com.yswl.yeeduo.http.KAsyncTask.TaskDelegate;
import com.yswl.yeeduo.tool.KStringTool;
import com.yswl.yeeduo.ui.base.BaseActivity;
import com.yswl.yeeduo.ui.exception.NetworkException;
import com.yswl.yeeduo.ui.task.ForgetPwdTask;
import com.yswl.yeeduo.ui.task.SendSMSCodeTask;
import com.yswl.yeeduo.ui.tool.ServiceAuthManager;
import com.yswl.yeeduo.ui.tool.ToastUtil;

public class ForgetPwdActivity extends BaseActivity implements OnClickListener, TaskDelegate {

	EditText mPhone;
	EditText mRePwd;
	EditText mCode;
	Button mSend;
	Button mConfirm;
	String userName;
	String userPwd;
	TimeCount mCount;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_forgetpwd);
		setHeader("忘记密码");
		initView();

	}

	private void initView() {
		mPhone = (EditText) findViewById(R.id.phone_et);
		mRePwd = (EditText) findViewById(R.id.pwd_edit);
		mCode = (EditText) findViewById(R.id.ca_send_code);
		mSend = (Button) findViewById(R.id.send_btn);
		mSend.setOnClickListener(this);
		mConfirm = (Button) findViewById(R.id.confirm_btn);
		mConfirm.setOnClickListener(this);
		
		mCount = new TimeCount(60 * 1000, 1000);
	}

	@Override
	public void onClick(View v) {
		userName = mPhone.getText().toString().trim();
		if (KStringTool.isEmpty(userName)) {
			mPhone.setError("手机号不能为空");
			return;
		}
		switch (v.getId()) {
		case R.id.send_btn:// 发送验证码
			Map<String, String> map2 = new TreeMap<String, String>();
			map2.put("phone", userName);
			map2.put("api_key", ServiceAuthManager.getToken(this));
			new SendSMSCodeTask(this, 10020, this).execute(map2);
			mCount.start();
			break;
		case R.id.confirm_btn:// 提交
			userPwd = mRePwd.getText().toString().trim();
			String userCode = mCode.getText().toString().trim();
			

			if ( KStringTool.isNotEmpty(userPwd) && KStringTool.isNotEmpty(userCode)) {
				Map<String, String> map = new TreeMap<String, String>();
				map.put("api_key", ServiceAuthManager.getToken(this));
				map.put("phone", userName);
				map.put("passwd", userPwd);
				map.put("code", userCode); // 验证码参数
				new ForgetPwdTask(this, 10021, this).execute(map);
			}

			break;

		default:
			break;
		}
	}

	/**
	 * 倒计时的内部类
	 * 
	 */
	public class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			mSend.setClickable(false);// 设置不可点击
			mSend.setText("倒计时：" + millisUntilFinished / 1000 + "秒");
		}

		@Override
		public void onFinish() {
			mSend.setClickable(true);// 倒数一秒的时候 获得点击
			mSend.setText("获取验证码");
		}

	}

	@Override
	public void faild_TaskDelegate(int taskId, Object object) {
		if (object instanceof NetworkException) {
			ToastUtil.showShortMsg("网络异常");
			return;
		}
		if (taskId == 10020) {

		} else {
			ToastUtil.showShortMsg("密码申请失败");
		}
	}

	@Override
	public void success_TaskDelegate(int taskId, Object object) {
		if (taskId == 10020) {
			ToastUtil.showShortMsg("验证码获取成功");
		} else if (taskId == 10021) {
			App.getInstance().isLogin = false;
			ServiceAuthManager.saveAuthParams(this, "", "", "");
			ToastUtil.showShortMsg("请重新登录");
			finish();
		}
	}

	
	// Um
		public void onResume() {
			super.onResume();
			MobclickAgent.onPageStart(this.getClass().getSimpleName());
			MobclickAgent.onResume(this); // 统计时长
		}

		public void onPause() {
			super.onPause();
			MobclickAgent.onPageEnd(this.getClass().getSimpleName());
			MobclickAgent.onPause(this);
		}
}
